package org.example.studentsystem_back.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jakarta.annotation.Resource;
import org.example.studentsystem_back.common.CommonResponse;
import org.example.studentsystem_back.domain.Dorm;
import org.example.studentsystem_back.domain.DormStu;
import org.example.studentsystem_back.domain.Notifications;
import org.example.studentsystem_back.dto.PageDTO;
import org.example.studentsystem_back.mapper.DormMapper;
import org.example.studentsystem_back.service.DormStuService;
import org.example.studentsystem_back.mapper.DormStuMapper;
import org.example.studentsystem_back.vo.DormStuMangeVo.DormStuIdVo;
import org.example.studentsystem_back.vo.DormStuMangeVo.DormStuVo;
import org.springframework.data.domain.jaxb.SpringDataJaxb;
import org.springframework.stereotype.Service;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.ZonedDateTime;
import java.util.*;

/**
* @author 马鑫
* @description 针对表【dorm_stu】的数据库操作Service实现
* @createDate 2024-07-12 13:29:22
*/
@Service
public class DormStuServiceImpl extends ServiceImpl<DormStuMapper, DormStu>
    implements DormStuService{
    @Resource
    private DormStuMapper dormStuMapper;
    @Resource
    private DormMapper dormMapper;

    @Override
    public CommonResponse<DormStuVo> findAll(PageDTO pageDTO,String id,int num,String name) throws ParseException {
        Page<DormStu> page = new Page<>(pageDTO.getPage(), pageDTO.getLimit());
        QueryWrapper<DormStu> queryWrapper = new QueryWrapper<>();
        queryWrapper.isNotNull("state");
        if(id!=null&& !id.isEmpty()){
            queryWrapper.eq("stu_num", id);
        }
        if(num>0)
        {
            queryWrapper.eq("dorm_num", num);
        }
        queryWrapper.like("area", name);
        queryWrapper.orderByAsc("area");
        queryWrapper.orderByAsc("dorm_num");
        List<DormStu> dormStuList=dormStuMapper.selectPage(page,queryWrapper).getRecords();
        DormStuVo dormStuVo=new DormStuVo();
        List<DormStuIdVo> dormStuIdVoList=new ArrayList<>();
        int i=pageDTO.getPage()*10-9;
        for(DormStu dormStu:dormStuList){
            DormStuIdVo vo=new DormStuIdVo();
            vo.setStuId(i);
            if(dormStu.getPhone()!=null)
            {
                dormStu.setPhone(dormStu.getPhone().substring(0,10));
            }
            vo.setDormStuId(dormStu);
            dormStuIdVoList.add(vo);
            i++;
        }
        int total= Math.toIntExact(dormStuMapper.selectCount(queryWrapper));
        dormStuVo.setTotal(total);
        dormStuVo.setDormStuIdVoList(dormStuIdVoList);
        return CommonResponse.createForSuccess("查询成功",dormStuVo);
    }

    @Override
    public CommonResponse<DormStu> add(DormStu dormStu) throws ParseException {
        QueryWrapper<Dorm> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("dorm_num", dormStu.getDormNum());
        queryWrapper.eq("area", dormStu.getArea());
        Dorm dorm=dormMapper.selectOne(queryWrapper);
        if(dorm==null||dorm.getEmptyNum()==0)
        {
            return CommonResponse.createForError("无空余床位");
        }
        dorm.setEmptyNum(dorm.getEmptyNum()-1);
        dormMapper.updateById(dorm);
        QueryWrapper<DormStu> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.eq("dorm_num", dormStu.getDormNum());
        queryWrapper1.eq("area", dormStu.getArea());
        queryWrapper1.eq("name", dormStu.getName());
        DormStu dormStu1=dormStuMapper.selectOne(queryWrapper1);
        if(dormStu1==null)
        {
            dormStuMapper.insert(dormStu);
        }
        else{
            dormStu1.setStuNum(dormStu.getStuNum());
            dormStu1.setState(dormStu.getState());
            dormStu1.setPhone(dormStu.getPhone());
            dormStuMapper.updateById(dormStu1);
        }
        return CommonResponse.createForSuccess("增加成功",dormStu);
    }

    @Override
    public CommonResponse<DormStu> update(DormStu dormStu) {
//        QueryWrapper<DormStu> queryWrapper = new QueryWrapper<>();
//        queryWrapper.eq("dorm_stu_id", dormStu.getDormStuId());
//        DormStu dormStu1=dormStuMapper.selectOne(queryWrapper);
//        dormStu.setReason(dormStu1.getReason());
//        dormStu.setDate(dormStu1.getDate());
        if(dormStu.getPhone()==null)
        {
            UpdateWrapper<DormStu> updateWrapper = new UpdateWrapper<>();
            updateWrapper.eq("dorm_stu_id", dormStu.getDormStuId());
            updateWrapper.set("phone", null);
            dormStuMapper.update(updateWrapper);
        }
        dormStuMapper.updateById(dormStu);
        return CommonResponse.createForSuccess("更新成功",dormStu);
    }

    @Override
    public CommonResponse<DormStu> delete(DormStu dormStu) {
        QueryWrapper<Dorm> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("dorm_num", dormStu.getDormNum());
        queryWrapper.eq("area", dormStu.getArea());
        Dorm dorm=dormMapper.selectOne(queryWrapper);
        dorm.setEmptyNum(dorm.getEmptyNum()+1);
        dormMapper.updateById(dorm);
        UpdateWrapper<DormStu> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("dorm_stu_id", dormStu.getDormStuId());
        updateWrapper.set("state",null);
        updateWrapper.set("phone",null);
        dormStuMapper.update(updateWrapper);
        return CommonResponse.createForSuccess("删除成功",dormStu);
    }

    @Override
    public CommonResponse<DormStuVo> findAll1(PageDTO pageDTO,int num,String name) throws ParseException {
        Page<DormStu> page = new Page<>(pageDTO.getPage(), pageDTO.getLimit());
        QueryWrapper<DormStu> queryWrapper = new QueryWrapper<>();
        queryWrapper.isNotNull("reason");
        if(num>0)
        {
            queryWrapper.eq("dorm_num", num);
        }
        queryWrapper.like("area", name);
        queryWrapper.orderByAsc("area");
        queryWrapper.orderByAsc("dorm_num");
        List<DormStu> dormStuList=dormStuMapper.selectPage(page,queryWrapper).getRecords();
        DormStuVo dormStuVo=new DormStuVo();
        List<DormStuIdVo> dormStuIdVoList=new ArrayList<>();
        int i=pageDTO.getPage()*10-9;
        for(DormStu dormStu:dormStuList){
            DormStuIdVo vo=new DormStuIdVo();
            vo.setStuId(i);
            if(dormStu.getDate()!=null)
            {
                dormStu.setDate(dormStu.getDate().substring(0,10));
            }
            vo.setDormStuId(dormStu);
            dormStuIdVoList.add(vo);
            i++;
        }
        int total= Math.toIntExact(dormStuMapper.selectCount(queryWrapper));
        dormStuVo.setTotal(total);
        dormStuVo.setDormStuIdVoList(dormStuIdVoList);
        return CommonResponse.createForSuccess("查询成功",dormStuVo);
    }

    @Override
    public CommonResponse<DormStu> add1(DormStu dormStu) {
        QueryWrapper<DormStu> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.eq("dorm_num", dormStu.getDormNum());
        queryWrapper1.eq("area", dormStu.getArea());
        queryWrapper1.eq("name", dormStu.getName());
        DormStu dormStu1=dormStuMapper.selectOne(queryWrapper1);
        if(dormStu1==null)
        {
            dormStuMapper.insert(dormStu);
        }
        else{
            dormStu1.setReason(dormStu.getReason());
            dormStu1.setDate(dormStu.getDate());
            dormStuMapper.updateById(dormStu1);
        }
        return CommonResponse.createForSuccess("增加成功",dormStu);
    }

    @Override
    public CommonResponse<DormStu> update1(DormStu dormStu) {
//        QueryWrapper<DormStu> queryWrapper = new QueryWrapper<>();
//        queryWrapper.eq("dorm_stu_id", dormStu.getDormStuId());
//        DormStu dormStu1=dormStuMapper.selectOne(queryWrapper);
//        dormStu.setReason(dormStu1.getReason());
//        dormStu.setDate(dormStu1.getDate());
        dormStuMapper.updateById(dormStu);
        return CommonResponse.createForSuccess("更新成功",dormStu);
    }

    @Override
    public CommonResponse<DormStu> delete1(DormStu dormStu) {
        UpdateWrapper<DormStu> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("dorm_stu_id", dormStu.getDormStuId());
        updateWrapper.set("reason",null);
        updateWrapper.set("date",null);
        dormStuMapper.update(updateWrapper);
        return CommonResponse.createForSuccess("删除成功",dormStu);
    }
}




